using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace AccessingMedia.ViewModels;

[ObservableObject]
public partial class MainPageViewModel {
    [ObservableProperty] private ImageSource _imageSource;

    [RelayCommand]
    private async Task PickAsync() {
        var photo = await MediaPicker.Default.PickPhotoAsync();

        if (photo is null) {
            return;
        }

        ImageSource = ImageSource.FromFile(photo.FullPath);
    }

    [RelayCommand]
    private async Task CaptureAsync() {
        var photo = await MediaPicker.Default.CapturePhotoAsync();

        if (photo is null) {
            return;
        }

        var localFilePath =
            Path.Combine(FileSystem.CacheDirectory, photo.FileName);
        await using var sourceStream = await photo.OpenReadAsync();
        await using var localFileStream = File.OpenWrite(localFilePath);
        await sourceStream.CopyToAsync(localFileStream);

        ImageSource = ImageSource.FromFile(localFilePath);
    }
}